import 'package:bot_toast/bot_toast.dart';
import 'package:easy_read_comic/internal/api/book_api.dart';
import 'package:easy_read_comic/internal/api/comic_api.dart';
import 'package:easy_read_comic/models/comic_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:share_plus/share_plus.dart';

import '../../models/chapter_model.dart';
import '../../models/page_model.dart';

class DetailController extends GetxController {
  final ScrollController scrollController = ScrollController();

  bool exceedHeight = false;

  ComicModel info = ComicModel(0, "正在加载...", "");

  List<ChapterModel> chapterList = [];

  //章节当前的页码
  int currentPage = 0;

  // 章节是否加载完毕
  bool chapterListLoadDone = false;

  void toTop() {
    scrollController.animateTo(
      0.0,
      duration: Duration(milliseconds: 500),
      curve: Curves.easeInOut,
    );
  }

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(this.listenScroll);
    this.resetLoadMore();
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.removeListener(this.listenScroll);
  }

  void loadMore() async {
    this.getChapterList();
  }

  void resetLoadMore() async {
    this.info = ComicModel(0, "正在加载...", "");
    this.chapterList = [];
    this.chapterListLoadDone = false;
    this.currentPage = 0;
    this.getComicDetail();
    this.getChapterList();
  }

  void listenScroll() {
    this.exceedHeight = scrollController.position.pixels > 400;
    update();
  }

  void getComicDetail() {
    var comicId = Get.parameters["comic_id"];
    ComicApi().detail(comicId.toString()).then((response) {
      if (response.data["code"] == 1) {
        this.info = ComicModel.formJson(response.data["data"]);
        this.update();
      }
    });
  }

  void getChapterList() {
    if (this.chapterListLoadDone) {
      BotToast.showText(text: "已加载完全部");
      return;
    }
    ComicApi()
        .chapterList(Get.parameters["comic_id"].toString(),
            currentPage: this.currentPage)
        .then((response) {
      if (response.data["code"] == 1) {
        var data = response.data["data"];
        for (var item in data["List"] ?? []) {
          this.chapterList.add(ChapterModel.formJson(item));
        }
        var page = PageModel.fromJson(data["Page"]);
        if (page.hasNextPage) {
          this.currentPage = page.NextPage;
        } else {
          this.chapterListLoadDone = true;
        }
        this.update();
      }
    });
  }

  void favourite(int comicId) {
    BookApi().favourite(comicId).then((response) {
      if (response.data["code"] == 1) {
        BotToast.showText(text: response.data["msg"]);
        this.info.IsFavourite = this.info.IsFavourite == 1 ? 0 : 1;
        this.update();
      }
    });
  }

  void share() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    Share.share(
      this.info.Intro.substring(0, 100),
      subject: "${this.info.Title} 分享来至 ${packageInfo.appName}",
    );
  }
}
